package cn.ctyun.ipush.dao;

import cn.ctyun.ipush.model.PictureModel;
import cn.ctyun.ipush.utils.DateUtils;
import cn.ctyun.ipush.vo.PictureVo;
import com.dexcoder.dal.JdbcDao;
import com.dexcoder.dal.build.Criteria;
import com.dexcoder.dal.spring.page.PageControl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: user
 * Date: 16-6-18
 * Time: 下午12:56
 * To change this template use File | Settings | File Templates.
 */
@Component
public class PictureDao {

    @Autowired
    private JdbcDao jdbcDao;

    @Autowired
    private PageControl pageControl;

    /**
     * 保存
     *
     * @param pictureModel
     */
    public void save(PictureModel pictureModel) {
        jdbcDao.save(pictureModel);
    }

    /**
     * 更新 ,主键Id 必须存在;此数据是修改后的完整对象
     *
     * @param pictureModel
     */
    public int update(PictureModel pictureModel) {
        if (StringUtils.isEmpty(pictureModel.getPictureModelId()))
            throw new RuntimeException("pictureModel 主键id 必须设置");
        pictureModel.setModifyDate(DateUtils.dateToString(new Date()));
        return jdbcDao.update(pictureModel);
    }


    /**
     * 更新 主键Id 必须存在
     *
     * @param pictureModelId
     */
    public int delete(String pictureModelId) {
        if (StringUtils.isEmpty(pictureModelId))
            throw new RuntimeException("pictureModel 主键id 必须设置");
        Criteria criteria = Criteria.update(PictureModel.class).where("pictureModelId", new Object[]{pictureModelId}).set("isDeleted", "1");
        return jdbcDao.update(criteria);
    }

    /**
     * 查询单个,根据ID
     *
     * @param pictureModelId
     * @return
     */
    public PictureModel querySingleResult(String pictureModelId) {
        if (StringUtils.isEmpty(pictureModelId))
            throw new RuntimeException("pictureModel 主键id 必须设置");
        Criteria criteria = Criteria.select(PictureModel.class).where("pictureModelId", new Object[]{pictureModelId}).where("and isDeleted", new Object[]{"0"});
        return jdbcDao.querySingleResult(criteria);
    }

    /**
     * 查询单个，根据名称
     *
     * @param pictureName
     * @return
     */
    public PictureModel querySingleResultByName(String pictureName) {
        if (StringUtils.isEmpty(pictureName))
            throw new RuntimeException("pictureName 为空");
        Criteria criteria = Criteria.select(PictureModel.class).where("pictureName", new Object[]{pictureName}).where("and isDeleted", new Object[]{"0"});
        return jdbcDao.querySingleResult(criteria);
    }

    /**
     * 模糊查询多个
     *
     * @param criteria
     * @return
     */
    public List<PictureModel> queryList(Criteria criteria) {
        return jdbcDao.queryList(criteria);
    }

    /**
     * 查询全部
     *
     * @param pictureModel
     * @return
     */
    public List<PictureModel> queryAll(PictureModel pictureModel) {
        return jdbcDao.queryList(pictureModel);
    }

    /**
     * 分页按条件查询
     *
     * @param currentPage
     * @param itemPages
     * @param pictureVo
     * @return
     */
    public List<PictureModel> queryListByPage(int currentPage, int itemPages, PictureVo pictureVo) {
        pageControl.performPage(currentPage, itemPages);
        Criteria criteria = Criteria.select(PictureModel.class).where("pictureName", " like ", new Object[]{"%t%"});
        jdbcDao.queryList(criteria);
        return pageControl.getPager().getList(PictureModel.class);
    }

    public List<PictureModel> queryListByPageWithUserId(int currentPage, int itemPages, PictureVo pictureVo) {
        pageControl.performPage(currentPage, itemPages);
        Criteria criteria = Criteria.select(PictureModel.class).where("pictureName", " like ", new Object[]{"%t%"});
        jdbcDao.queryList(criteria);
        return pageControl.getPager().getList(PictureModel.class);
    }

    public List<PictureModel> queryBySql( PictureModel pictureModel) {
        //pageControl.performPage(1, 10);
        pictureModel.setPictureName("testdb");
        List<PictureModel> pictureModelList = jdbcDao.queryListForSql("cn.ctyun.ipush.model.PictureModel.getData", "params", new Object[]{pictureModel, null}, PictureModel.class);
        return pictureModelList;
    }
}
